unit oEditorRunPropertiesManager;

interface

uses
  oManager, oProcessItem, oRunPropertiesManager, oProcessObjects, oFrameRunProperties,
  SysUtils, oProcessObjectConstants, oFormRunProperties;

type
  ERunPropertiesEditorManager = class(Exception);

  TEditorRunPropertiesManager = class(TManager)
    private
      aFrame : TfRunProperties;
      form : TFormRunProp;
      procedure show(paRunProperties : TRunPropertiesManager.TRunPropertiesType);
      procedure clear;
      function getRunProperties : TRunPropertiesData;
    public
      constructor Create(paMaster: TManager);
      destructor Destroy; override;
      function ProcessObject(paItem: TProcessItem):TProcessItem; override; final;
  end;

implementation

{ TRunPropertiesEditorManager }

resourcestring eEdRunManNotFound = 'run config not found';

procedure TEditorRunPropertiesManager.clear;
var
  i: Integer;
begin
  with aFrame do begin
    txtName.Text := '';
    txtLength.Text := '0';
    txtReplications.Text := '0';
    for i := 0 to clbxStats.Count - 1 do clbxStats.Checked[i] := false;
  end;
end;

constructor TEditorRunPropertiesManager.Create(paMaster: TManager);
begin
  aMaster := paMaster;
  form := nil;
  aFrame := nil;
end;

destructor TEditorRunPropertiesManager.Destroy;
begin
  aFrame.Free;
  inherited;
end;

function TEditorRunPropertiesManager.getRunProperties: TRunPropertiesData;
var
  data : TRunPropertiesData;
  i: Integer;
begin
  data := TRunPropertiesData.Create('',0,0);
  try
    with aFrame do begin
      data.NameData := txtName.Text;
      data.Replications := StrToFloat(txtReplications.Text);
      data.Length := StrToFloat(txtLength.Text);
      data.Stats.Clear;
      for i := 0 to clbxStats.Count - 1 do data.Stats.Add(clbxStats.Checked[i]);
      Result := data;
    end;
  except
    on e : Exception do raise ERunPropertiesEditorManager.Create('Error on getting data');
    on pe : ERunPropertiesEditorManager do
     begin
       data.Free;
       Result := nil;
     end;
  end;
end;

function TEditorRunPropertiesManager.ProcessObject(
  paItem: TProcessItem): TProcessItem;
var
  data : TRunPropertiesData;
  item: TProcessItem;
begin

   case paItem.Code of
    PO_RUNPROP_EDITOR_INSERT : begin
        form := TFormRunProp.Create(nil);
        form.Master := aMaster;
        aFrame := form.aFrame;
        aFrame.txtName.Enabled := true;
        clear;
        aFrame.Editing := false;
        aFrame.Master := aMaster;
        form.Show;
        Result := nil;
    end;

    PO_RUNPROP_EDITOR_EDIT : begin
        form := TFormRunProp.Create(nil);
        form.Master := aMaster;
        aFrame := form.aFrame;
        aFrame.Editing := true;
        aFrame.Visible := true;
        aFrame.txtName.Enabled := false;
        item := Master.ProcessObject(TProcessItem.create(PO_RUNPROPMAN_EXISTS,paItem.Data as TString));
        if item.Data = nil then raise ERunPropertiesEditorManager.Create(eEdRunManNotFound);
        show(item.Data as TRunPropertiesManager.TRunPropertiesType);
        Result := nil;
    end;

    PO_RUNPROP_EDITOR_GET : begin
        if aFrame.Editing
          then aMaster.ProcessObject(TProcessItem.create(PO_RUNPROPMAN_EDIT,getRunProperties))
          else aMaster.ProcessObject(TProcessItem.create(PO_RUNPROPMAN_INSERT,getRunProperties));
        Result := nil;
        form.Close;
//        form.Free;    d(^_^)b
        aFrame := nil;
        form := nil;
    end
    else
      raise ERunPropertiesEditorManager.Create('Could not process code');
      Result := nil;
   end;
end;

procedure TEditorRunPropertiesManager.show(paRunProperties: TRunPropertiesManager.TRunPropertiesType);
var
  i: Integer;
begin
  try
    //form := TFormRunProp.Create(nil);
    //aFrame := form.aFrame;
    clear;
    aFrame.Master := aMaster;
    with paRunProperties do
     with aFrame do begin
      txtName.Text := paRunProperties.Name;
      txtLength.Text := FloatToStr(paRunProperties.Length);
      txtReplications.Text := FloatToStr(paRunProperties.Replications);
      for i := 0 to paRunProperties.Stats.Count - 1 do
        clbxStats.Checked[i] := paRunProperties.Stats[i];
     end;
     form.ShowModal;
  except
    on e : Exception do ;//raise ERunPropertiesEditorManager.Create('Error on display');
  end;
end;

end.
